package cartagows.wsframework.core;

import java.lang.reflect.Field;
import java.util.ArrayList;

import cartagows.CartagowsParam;

/**
 * Singleton thread-safe used for the validation of the operationID
 * @author Andrea Santi
 *
 */
public class FrameworkIDValidator {
	
	private ArrayList<String> specificationList;
	
	private static class Container {
		public static FrameworkIDValidator instance = new FrameworkIDValidator();
	}
	
	public static FrameworkIDValidator getInstance(){
		return Container.instance;
	}

	private FrameworkIDValidator(){

		try {

			specificationList = new ArrayList<String>();
			Class<?> specClass = Class.forName(CartagowsParam.IOPERATION_CLASS_PATH);

			for (Field operationField : specClass.getFields())
				specificationList.add(operationField.getName());
		} 
		catch (Exception ex) 
		{
		}		
	}
	
	public boolean validateOperation(Class<?> specificationClass, int operationID){
	
		boolean isIDCheckPassed = false;
		
		try{
			
			for (Field operationField : specificationClass.getFields()){

				if((operationField.getInt(specificationClass) == operationID) && (!specificationList.contains(operationField.getName()))){
					isIDCheckPassed = true;
					break;
				}
			}
		}
		catch(Exception ex){
			
		}
		return isIDCheckPassed;
	}
	
	public boolean validateSpecification(int specificationID){
		
		boolean isIDCheckPassed = false;

		try{
			
			Class<?> specClass = Class.forName(CartagowsParam.IOPERATION_CLASS_PATH);
			
			for (Field specificationField : specClass .getFields()){
				if(specificationField.getInt(specClass) == specificationID){
					isIDCheckPassed = true;
					break;
				}
			}
		}
		catch(Exception ex){
		}
		return isIDCheckPassed;
	}
}